package com.markul.android.logic.model

import androidx.lifecycle.LiveData
import com.markul.android.MarkulApplication
import com.markul.android.MarkulApplication.Companion.context
import com.markul.android.logic.model.room.*
import com.markul.android.logic.room.*
import java.util.*
import kotlin.collections.ArrayList


object Repository {
    val appDao = AppDatabase.getDatabase(context).appDao()
    val publicDao = AppDatabase.getDatabase(context).publicDao()

    //    val allAlbums: LiveData<OwnerWithAlbums> = loadAllAlbumsByLiveData(MarkulApplication.userId)
    val allAlbums: LiveData<List<Album>> = loadAllAlbumsByLiveData(MarkulApplication.userId)

    val allAlbum: LiveData<List<Album>> =loadAlbumsByLiveData(MarkulApplication.userId)

    val allPhotos: LiveData<List<AlbumWithPhotos>> = loadAllPhotosByLiveData(MarkulApplication.userId)

    lateinit var allUser:LiveData<List<UserAndPublicRoomCrossRef>>

    val allPRoom:LiveData<List<UserAndPublicRoomCrossRef>> = loadUserWithPublicRoomsByLiveData(MarkulApplication.userId)

    var allPAlbums:LiveData<List<PublicAlbum>> =loadPublicRoomWithPublicAlbumsByLiveData(MarkulApplication.publicRoomId)

//增加
    fun addUser(user: User) {
        appDao.addUser(user)
    }

    fun addAlbum(album: Album) {
        appDao.addAlbum(album)
    }

    fun addPhoto(photo: Photo) {
        appDao.addPhoto(photo)
    }

    fun addPublicRoom(publicRoom: PublicRoom){
        publicDao.addPublicRoom(publicRoom)
    }

    fun addPublicAlbum(publicAlbum: PublicAlbum) {
        publicDao.addPublicAlbum(publicAlbum)
    }

    fun addPublicPhoto(publicPhoto: PublicPhoto) {
        publicDao.addPublicPhoto(publicPhoto)
    }

    fun addCrossRef(addUserAndPublicRoomCrossRef: UserAndPublicRoomCrossRef) {
        publicDao.addUserAndPublicRoomCrossRef(addUserAndPublicRoomCrossRef)
    }

    //删除
    fun deleteAlbumAndPhotos(albumId:Int){
        appDao.deleteAlbum(albumId)
        appDao.deletePhotos(albumId)
    }

//    private fun loadAllAlbumsByLiveData(userId: Int): LiveData<OwnerWithAlbums> {
//        return appDao.getOwnerWithAlbumsByLiveData(userId)
//    }

    private fun loadAllPhotosByLiveData(albumOwnerId: Int): LiveData<List<AlbumWithPhotos>> {
        return appDao.getAlbumWithPhotos(albumOwnerId)
    }

    private fun loadAllAlbumsByLiveData(userId: Int): LiveData<List<Album>> {
        return appDao.getOwnerWithAlbums(userId)
    }

    private fun loadAlbumsByLiveData(userId: Int): LiveData<List<Album>> {
        return appDao.getAlbums(userId)
    }

    //查找公共空间中的全部用户
    private fun loadPublicRoomWithUsers(pRoomId:Int):LiveData<List<UserAndPublicRoomCrossRef>>{
        return publicDao.getPublicRoomWithUsers(pRoomId)
    }

    fun loadPublicRoomWithUsersByLiveData(pRoomId:Int):LiveData<List<UserAndPublicRoomCrossRef>>{
        allUser=loadPublicRoomWithUsers(pRoomId)
        return allUser
    }

//查找用户所有的全部公共空间
    private fun loadUserWithPublicRoomsByLiveData(userId: Int):LiveData<List<UserAndPublicRoomCrossRef>>{
        return publicDao.getUserWithPublicRooms(userId)
    }

//查找公共空间中的公共相册
    private fun loadPublicRoomWithPublicAlbumsByLiveData(pRoomId:Int):LiveData<List<PublicAlbum>>{
        return publicDao.getPublicRoomWithPublicAlbums(pRoomId)
    }




}


